package com.xiaoyu.lanling.feature.im.request

import `in`.srain.cube.request.JsonData
import com.xiaoyu.im.ChatToken
import com.xiaoyu.im.datamodel.MessagePayload
import com.xiaoyu.im.request.MessageRequest
import com.xiaoyu.lanling.common.Constant.MessageAttrsKey.GIFT
import com.xiaoyu.lanling.common.Constant.MessageAttrsKey.LOCAL_FIRST_PICTURE
import com.xiaoyu.lanling.common.Constant.MessageAttrsKey.THUMB_HEIGHT
import com.xiaoyu.lanling.common.Constant.MessageAttrsKey.THUMB_WIDTH
import com.xiaoyu.lanling.feature.chat.model.message.MessageType.MessageTypeDef
import com.xiaoyu.lanling.feature.chat.model.message.MessageType.TYPE_1000_TEXT
import com.xiaoyu.lanling.feature.chat.model.message.MessageType.TYPE_1001_IMAGE
import com.xiaoyu.lanling.feature.chat.model.message.MessageType.TYPE_1002_AUDIO
import com.xiaoyu.lanling.feature.chat.model.message.MessageType.TYPE_1003_GIFT
import com.xiaoyu.lanling.feature.chat.model.message.MessageType.TYPE_1004_CALL
import com.xiaoyu.lanling.feature.chat.model.message.MessageType.TYPE_1005_VIDEO
import com.xiaoyu.lanling.feature.chat.model.videomodel.ChatVideoModel

class MessageRequestBuilder private constructor(private val chatToken: ChatToken, private val fromType: String) {

    @MessageTypeDef
    private var mType = 0
    private var mText: String? = null
    private var mImageList: List<String>? = null
    private var mLocalFilePath: String? = null
    private var mDuration = 0
    private var mGiftJson: JsonData? = null
    private var mVoiceStatusType: String? = null
    private var mVoiceStatusCallDuration = 0L
    private var mVideoModel: ChatVideoModel? = null

    fun text(text: String?): MessageRequestBuilder {
        mType = TYPE_1000_TEXT
        mText = text
        return this
    }

    fun imageList(imageList: List<String>): MessageRequestBuilder {
        mType = TYPE_1001_IMAGE
        mImageList = imageList
        return this
    }

    fun image(localFilePath: String): MessageRequestBuilder {
        mType = TYPE_1001_IMAGE
        mLocalFilePath = localFilePath
        return this
    }

    fun audio(filePath: String, duration: Int): MessageRequestBuilder {
        mType = TYPE_1002_AUDIO
        mLocalFilePath = filePath
        mDuration = duration
        return this
    }

    fun video(videoModel: ChatVideoModel): MessageRequestBuilder {
        mType = TYPE_1005_VIDEO
        mLocalFilePath = videoModel.filePath
        mDuration = videoModel.duration
        mVideoModel = videoModel
        return this
    }

    fun gift(giftJson: JsonData): MessageRequestBuilder {
        mType = TYPE_1003_GIFT
        mGiftJson = giftJson
        return this
    }

    fun callStatus(type: String, callDuration: Long): MessageRequestBuilder {
        mType = TYPE_1004_CALL
        mVoiceStatusType = type
        mVoiceStatusCallDuration = callDuration
        return this
    }

    fun buildAndSend() {
        mImageList?.forEach { path ->
            newBuilder(chatToken, fromType).image(path).buildAndSendSingle()
        } ?: buildAndSendSingle()
    }

    private fun buildAndSendSingle() {
        val payload = MessagePayload.obtain(mType)
        val messageRequest = MessageRequest.create(chatToken, payload)
        messageRequest.fromType = fromType
        payload.putAttrs("uniqueKey", messageRequest.pendingMessage.localId)
        when (mType) {
            TYPE_1000_TEXT -> payload.text = mText
            TYPE_1001_IMAGE -> payload.filePath = mLocalFilePath
            TYPE_1002_AUDIO -> {
                payload.filePath = mLocalFilePath
                payload.duration = mDuration
            }
            TYPE_1005_VIDEO -> {
                payload.filePath = mLocalFilePath
                payload.duration = mDuration
                payload.putAttrs(LOCAL_FIRST_PICTURE, mVideoModel!!.firstPicture)
                payload.putAttrs(THUMB_WIDTH, mVideoModel!!.thumbWidth)
                payload.putAttrs(THUMB_HEIGHT, mVideoModel!!.thumbHeight)
            }
            TYPE_1003_GIFT -> {
                payload.putAttrs(GIFT, mGiftJson)
            }
            TYPE_1004_CALL -> {
                payload.putAttrs("type", mVoiceStatusType);
                payload.putAttrs("callDuration", mVoiceStatusCallDuration);
            }
        }
        messageRequest.enqueue()
    }

    companion object {
        fun newBuilder(chatToken: ChatToken, fromType: String): MessageRequestBuilder {
            return MessageRequestBuilder(chatToken, fromType)
        }
    }
}